package beans;

import java.util.ArrayList;
import java.util.List;

import models.Album;
import models.Article;
import models.Mail;
import models.Photo;
import models.Trend;
import models.User;

public class TrendBean {
	public long time;
	public String title;
	public String authorName;
	public String trendId;
	public String url;
    public String content;
	public int type;
    public int num;
	public static int ARTICLE = 0;
	public static int ALBUM = 1;
	public static TrendBean bulid(Trend trend)
	{
		TrendBean bean = new TrendBean();
		if(trend.type == Trend.ALBUM)
		{
			Album album = Album.findById(trend.object_id);
			if(album != null)
			{
				bean.authorName = User.getAuthorNameById(album.authorId);
				bean.time = album.createTime;
				bean.title = album.title;
				bean.trendId = album.id;
				bean.type = Trend.ALBUM;
                bean.content = album.info;
                bean.num=album.articleNum;
				Photo photo = Photo.find("id = ?",album.photoId).first();
		        if(photo!=null) {
		            bean.url = photo.getPhoto();
		        }
		        else{
		            bean.url = "";
		        }
			}
			else
			{
				bean = TrendBean.getDefaultOJ();
			}
		}
		else if(trend.type == Trend.ARTICLE)
		{
			Article article = Article.findById(trend.object_id);
			if(article != null)
			{
				bean.authorName = User.getAuthorNameById(article.authorId);
				bean.time = article.createTime;
				bean.title = article.title;
				bean.trendId = article.id;
				bean.type = Trend.ARTICLE;
                bean.content=article.content;
                bean.num =article.commentNum;
				Photo photo = Photo.find("id = ?",article.photoId).first();
		        if(photo!=null) {
		            bean.url = photo.getPhoto();
		        }
		        else{
		            bean.url = "";
		        }
			}
			else
			{
				bean = getDefaultOJ();
			}
		}
		return bean;
	}
	public static List<TrendBean> bulidList(List<Trend> trendlist){
		List<TrendBean> trendBeanList = new ArrayList<TrendBean>();
		for(int item =0 ; item < trendlist.size(); item ++){
			Trend trend = trendlist.get(item);
			TrendBean trendbean = TrendBean.bulid(trend);
                trendBeanList.add(trendbean);

		}
		return trendBeanList;
	}
	
	public static TrendBean getDefaultOJ()
	{
		TrendBean bean = new TrendBean();
		bean.authorName = "";
		bean.time = 0;
		bean.title = "";
		bean.trendId = "";
		bean.type = -1;
		bean.url = "";
		return bean;
	}
}
